package com.bidevalution.business.vote.callbackService.impl;

import com.alibaba.druid.util.StringUtils;
import com.bidevalution.business.enums.VoteOptionEnum;
import com.bidevalution.business.exception.VoteCallbackException;
import com.bidevalution.business.service.BeEvalutionOutcomeService;
import com.bidevalution.business.service.BeSectionExpertInfoService;
import com.bidevalution.business.service.BeSectionStepJobService;
import com.bidevalution.business.service.BeSectionStepService;
import com.bidevalution.business.vote.callbackService.VoteBaseCallback;
import com.bidevalution.business.vote.service.BeExpertVoteService;
import com.bidevalution.business.vote.vi.CallStepJobVi;
import com.bidevalution.dao.entity.*;
import com.bidevalution.dao.mapper.BeExpertVoteDetailMapper;
import com.bidevalution.dao.mapper.BeExpertVoteMapper;
import com.qtp.core.util.GsonUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Log4j2
@Component
public class UpdateResultCallback implements VoteBaseCallback {
    @Autowired
    private BeEvalutionOutcomeService beEvalutionOutcomeService;

    @Autowired
    private BeSectionStepJobService beSectionStepJobService;

    @Autowired
    private BeExpertVoteMapper beExpertVoteMapper;

    @Autowired
    private BeExpertVoteDetailMapper beExpertVoteDetailMapper;

    @Override
    public CallStepJobVi callback(List<BeExpertVoteCallBack> voteCallBackList)  throws VoteCallbackException {
        VoteParams voteParams = null;
        CallStepJobVi callStepJobVi = null;
        for(BeExpertVoteCallBack beExpertVoteCallBack : voteCallBackList) {
            String voteParamsStr = beExpertVoteCallBack.getVoteParams();
            String voteId = beExpertVoteCallBack.getSeqId();
            if (!StringUtils.isEmpty(voteParamsStr)) {
                voteParams = GsonUtil.parseJsonWithGson(voteParamsStr, VoteParams.class);

                for (BeExpertVoteDetailExt beExpertVoteDetailExt: beExpertVoteCallBack.getVoteDetailList()) {
                    String expertId = beExpertVoteDetailExt.getExpertId();
                    VoteOptionEnum voteOptionEnum = VoteOptionEnum.get(beExpertVoteDetailExt.getVoteResule());
                    voteParams.setResult(voteOptionEnum.getCodeStr());
                    voteParams.setExpertId(expertId);
                    // 更新子類
                    beEvalutionOutcomeService.updateChildrenResult(voteParams);
                    // 更新父类值
                    beEvalutionOutcomeService.updateResult(voteParams);
                }
            }
        }

        // 进行下一个
        BeExpertVoteCallBack beExpertVoteCallBack = voteCallBackList.get(0);
        BeExpertVote beExpertVote = beExpertVoteMapper.selectByPrimaryKey(beExpertVoteCallBack.getSeqId());

        callStepJobVi = (CallStepJobVi) GsonUtil.parseJsonWithGson(beExpertVote.getStepParams(), CallStepJobVi.class);

        return callStepJobVi;
    }
}
